<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\facade\Db;
use think\facade\Cache;
use think\facade\Config;
use think\facade\View;
use think\facade\Env;
use think\facade\Request;
class Base extends \app\admin\Base
{
  public $md, $con, $ac, $appPath,$cli,$header;
  public $menu, $app, $guid,$develop,$system;
  public $admin,$admin_id,$openid;
  public $appPlatform;

  public function initialize()
  {
    
    $request = request();
    $method = Request::method();
    if ($method == 'OPTIONS') {
      exit();
    }

    $this->md = app('http')->getName();
    $this->con = $request->controller();
    $this->ac = $request->action();
    $this->header = Request::header();

    if (isset($this->header['accesstoken'])) {
      $this->token = $this->header['accesstoken'];
      if ($this->token) {
        $this->admin = Db::name('admin')
                ->where('token',$this->token)
                ->find();
        if ($this->admin) {
          $this->admin_id = $this->admin['id'];
        }else {
          $resp['code'] = 402;
          $resp['msg'] = 'token 错误';
          echo json_encode($resp);
          exit();
        }
      }else {
        $resp['code'] = 402;
        $resp['msg'] = 'token 错误';
        echo json_encode($resp);
        exit();
      }
    }else {
      $resp['code'] = 402;
      $resp['msg'] = 'token 错误';
      echo json_encode($resp);
      exit();
    }

  }

  public function search()
  {
    
    $order = input('order','desc');
    $sort = input('sort','id');
    $searchKey = input('searchKey');
    $pageSize = input('pageSize/d',1);
    $limit = input('limit/d',10);
    $query = [];
    if (method_exists($this, '_filter')) {
      $query = $this->_filter();
    }
    // if ($searchKey) {
    //   $query[] = ['name|pid|id','like',"%{$searchKey}%"];
    // }
    // print_r($order);
    // print_r($sort);
    // exit();
    $rows = Db::name($this->con)
              ->where($query)
              ->order($sort.' '.$order)
              ->page($pageSize,$limit)
              // ->page($pageSize,10)
              ->select()->toArray();

    foreach ($rows as &$k) {
      if (method_exists($this, 'ckvo')) {
        $k = $this->ckvo($k);
      }
    }
    
    // print_r(Db::name('menu')->getLastSql());
    $sql = Db::name($this->con)->getLastSql();
    $total = Db::name($this->con)
              ->where($query)
              ->count('id');
    $data['page'] =1;
    $data['rows'] = $rows;
    $data['total'] = $total;
    $data['sql'] = $sql;

    return $this->doSuccess('ok',$data);
  }
  public function remove()
  {
    $id = input('id');
    $ret = Db::name($this->con)
              ->whereIn('id',$id)
              ->delete();
    if ($ret) {
      # code...
    }
    return $this->doSuccess('ok');
  }

  public function update()
  {
    $post = input('post.');
    $id = input('id');
    $pk = Db::name($this->con)
              ->where('id',$id)
              ->update($post);
    if ($pk > 0) {
      return $this->doSuccess('ok');
    }
    return $this->doSuccess('ok');
  }

  public function index()
  {
    return View::fetch();
  }

  public function add()
  {
    return View::fetch();
  }


  public function insert()
  {
    $form = input('post.');
    unset($form['id']);
    $form['ctime'] = time();
    $pk = Db::name($this->con)
              // ->where('id',$id)
              ->insert($form);
    return $this->doSuccess('ok');
    # code...
  }

  public function doError($msg = '')
  {
    $resp['code'] = 400;
    $resp['msg'] = $msg;
    $json = json($resp, 200);
    return $json;
  }

  public function doSuccess($msg = '', $data = '')
  {
    $resp['code'] = 200;
    $resp['msg'] = $msg;
    $resp['data'] = $data;
    $json = json($resp, 200);
    return $json;
  }
}
